package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DtsAdminServiceImpl implements DtsAdminService {
    BCryptPasswordEncoder pass = new BCryptPasswordEncoder();
    @Autowired
    DtsAdminMapper adminMapper;
    @Override
    public DtsAdmin findByUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername, username);
        DtsAdmin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String username, String sort, String order) {
        IPage<DtsAdmin> ipage = new Page<>(page,limit);
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)) {
            wrapper.like(username , username);
        }
        //添加排序条件
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order) ) {
            if("desc".equals(order)) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        adminMapper.selectPage(ipage, wrapper);
        long total = ipage.getTotal();
        List<DtsAdmin> items = ipage.getRecords();
        Map<String,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);
        return data;
    }
    @Override
    public DtsAdmin insert(DtsAdmin admin) {
        //对admin中的数据进行维护
        //对密码进行加密
        String encode = pass.encode(admin.getPassword());
        admin.setPassword(encode);
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        //新增操作
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        //在更新前对数据进行调整
        //根据id查询数据库中的密码，一致不用加密
        DtsAdmin dtsAdmin = adminMapper.selectById(admin.getId());
        if (!admin.getPassword().equals(dtsAdmin.getPassword())){
            admin.setPassword(pass.encode(admin.getPassword()));
        }
        admin.setUpdateTime(new Date());
        adminMapper.updateById(admin);
        return admin;
    }
    @Autowired
    DtsBrandMapper brandMapper;
    @Override
    public void delete(DtsAdmin admin) {
        //逻辑删除
//        admin.setDeleted(true);
//        adminMapper.updateById(admin);
        //删除前需要判断是否被其他对象所关联
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId,admin.getId());
        Integer count = brandMapper.selectCount(wrapper);
        if (count>0){
            throw new RuntimeException("管理员被品牌关联，不能删除");
        }
        adminMapper.deleteById(admin.getId());
    }
}
